<!--
	vDevCrudSample
-->
<?php
	/***************************************************
	 * View Declaration
	 ***************************************************/

		$header = array(
			'개발용 샘플 구현',
			'개발용 샘플 구현'
		);

		$formOption01 = sprintf('/%s/%s/',$this->nUri['seg1'],$this->nUri['txSeg']);
		$formOption02 = array(
			'role' => 'form',
			'id' => 'nForm',
			'class' => 'nForm'
		);

		$pk_uid = array(
			'type' => 'hidden',
			'id' => 'pk_uid',
			'defaultValue' => $this->gibberishaes->enc(@$r['pk_uid'], $this->config->item('aes_key'))
		);

		$st_text_field01 = array(
			'title' => '32자텍스트필드',
			'type' => 'text',
			'labelText' => '',
			'id' => 'st_text_field01',
			'placeholder' => '해당 필드를 입력하세요',
			'maxlength' => '32',
			'defaultValue' => @$r['st_text_field01']
		);

		$st_text_field02 = array(
			'title' => '자유형식 한줄',
			'type' => 'text',
			'labelText' => '',
			'id' => 'st_text_field02',
			'placeholder' => '해당 필드를 입력하세요',
			'defaultValue' => @$r['st_text_field02']
		);

		$no_text_field03 = array(
			'title' => '숫자타입',
			'type' => 'number',
			'labelText' => '',
			'id' => 'no_text_field03',
			'placeholder' => '10자리 이하 숫자를 입력하세요',
			'defaultValue' => @$r['no_text_field03']
		);

		$dt_date_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '날짜타입',
			'type' => 'datetime',
			'labelTextYMD' => '년월일',
			'labelTextHIS' => '시분초',
			'idYMD' => 'dt_date_field01',
			'idHIS' => 'tm_time_field01',
			'placeholderYMD' => '예시) 2014-01-01',
			'placeholderHIS' => '예시) 12:59:59',
			'defaultValueYMD' => @$r['dt_date_field01'],
			'defaultValueHIS' => @$r['tm_time_field01']
		);

		$pw_password_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '암호타입',
			'type' => 'password',
			'labelText' => '암호를 입력하세요',
			'id' => 'pw_password_field01',
			'placeholder' => '6~16자리의 ',
			'defaultValue' => @$r['pw_password_field01']
		);

		$fg_checkbox_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '체크박스',
			'id' => 'fg_checkbox_field01',
			'items' => array(
				'y' => 'Y'
			),
			'defaultValue' => @$r['fg_checkbox_field01']
		);

		$fg_radio_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '래디오',
			'id' => 'fg_radio_field01',
			'items' => array(
				'a' => 'A',
				'b' => 'B',
				'c' => 'C'
			),
			'defaultValue' => @$r['fg_radio_field01']
		);

		$fg_select_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '셀렉트',
			'id' => 'fg_select_field01',
			'group' => array(
				array(
					'outgroup' => '선택없음',
					'items' => array(
						'' => '없음',
					)
				),
				array(
					'outgroup' => 'a 부터 b',
					'items' => array(
						'a' => 'A',
						'b' => 'B'
					)
				),
				array(
					'outgroup' => 'c 부터 d',
					'items' => array(
						'c' => 'C',
						'd' => 'D'
					)
				)
			),
			'defaultValue' => @$r['fg_select_field01']
		);

		$fg_select_field02 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '셀렉트',
			'id' => 'fg_select_field02',
			'items' => array(
				''=>'없음',
				'a' => 'A',
				'b' => 'B'
			),
			'defaultValue' => @$r['fg_select_field02']
		);

		$tx_textarea_field01 = array(
			'createOnly' => $this->nhtml->getCreateOnly($mode),
			'title' => '텍스트에리어',
			'labelText' => '',
			'id' => 'tx_textarea_field01',
			'placeholder' => '텍스트를 입력하세요',
			'defaultValue' => @$r['tx_textarea_field01']
		);

		$nButtons01 = array(
			'layout' => 'horizontal',
			'items' => array(
				array(
					'type' => 'primary',
					'text' => '확인',
					'class' => 'uIdBtnConfirm01'
				),
				array(
					'type' => 'default',
					'text' => '취소',
					'class' => 'uIdBtnCancel01'
				)
			)
		);

		$nButtons02 = array(
			'layout' => 'horizontal-justified',
			'items' => array(
				array(
					'type' => 'primary',
					'text' => '확인',
					'class' => 'uIdBtnConfirm02'
				),
				array(
					'type' => 'default',
					'text' => '취소',
					'class' => 'uIdBtnCancel02'
				)
			)
		);

		$nButtons03 = array(
			'layout' => 'vertical',
			'items' => array(
				array(
					'type' => 'primary',
					'text' => '확인',
					'class' => 'uIdBtnConfirm03'
				),
				array(
					'type' => 'default',
					'text' => '취소',
					'class' => 'uIdBtnCancel03'
				)
			)
		);

		$tableOption = array(
			'th' => array ('번호','16자','자유형식','숫자','일자','시간','비밀번호','체크박스','레디오','셀렉트박스01','셀렉트박스02'),
			'cols' => array ('width:70px','','','width:70px','width:90px','width:80px','','width:70px','width:70px','width:100px','width:100px'),
			'uId' => 'uIdTable01'
		);

		$nToolbarButton01 = array(
			'text' => '새로운 레코드 생성',
			'type' => 'primary',
			'icon' => 'fa fa-plus',
			'class' => 'uIdNewRecord01',
			'link' => $this->nmover->jsLink(
				array(
					'seg1' => $this->nUri['seg1'],
					'seg2' => $this->nUri['createSeg'],
					'queryString' => array( 'mode'=>'create' ))
				)
		);

		$nToolbarButton02 = array(
			'text' => '목록으로',
			'type' => 'primary',
			'icon' => 'fa fa-arrow-left',
			'class' => 'uIdGoToList01',
			'link' => $this->nmover->jsBack()
		);

		$nToolbarButton03 = array(
			'text' => '삭제',
			'type' => 'default',
			'icon' => 'fa fa-trash-o',
			'class' => 'uIdDelete pull-right',
			'link' => 'void(0)'
		);

?><?php
	/***************************************************
	 * View Output
	 ***************************************************/
?>
	<?=$this->nhtml->nHeader($header);?>
	<div class="main-content">
		<?=$this->nhtml->nFormOpen($formOption01, $formOption02)?>

			<?php
				/***************************************************
	 			* 목록
	 			***************************************************/
			?>
			<?php if($mode == 'list'): ?>
				<?=$this->nhtml->nSubjection('목록')?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nBtnToolbarOpen(array('size'=>'6'))?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton01)?>
				<?=$this->nhtml->nBtnToolbarClose()?>

				<?=$this->nhtml->paginationContainer()?>
				<?=$this->nhtml->tableContainer($tableOption);?>
				<?=$this->nhtml->paginationContainer()?>
			<?php endif;?>

			<?php
				/***************************************************
	 			* 생성, 조회
	 			***************************************************/
			?>
			<?php if($mode == 'create' || $mode == 'retrieve') : ?>
				<?=$this->nhtml->nSubjection($subjection)?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nBtnToolbarOpen(array('size'=>'12'))?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton02)?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton03)?>
				<?=$this->nhtml->nBtnToolbarClose()?>

				<?=$this->nhtml->nHidden($pk_uid)?>
				<?=$this->nhtml->nInput($st_text_field01)?>
				<?=$this->nhtml->nInput($st_text_field02)?>
				<?=$this->nhtml->nInput($no_text_field03)?>
				<?=$this->nhtml->nDateTimeInput($dt_date_field01)?>
				<?=$this->nhtml->nInput($pw_password_field01)?>
				<?=$this->nhtml->nHr()?>

				<?=$this->nhtml->nCheckbox($fg_checkbox_field01)?>
				<?=$this->nhtml->nRadio($fg_radio_field01)?>
				<?=$this->nhtml->nSelect($fg_select_field01)?>
				<?=$this->nhtml->nSelect($fg_select_field02)?>
				<?=$this->nhtml->nHr()?>

				<?=$this->nhtml->nTextarea($tx_textarea_field01)?>
				<?=$this->nhtml->nHr()?>

				<?=$this->nhtml->nButtons($nButtons02)?>
				<?php if($mode == 'create'):?>
					<script type="text/javascript" charset="utf-8">$('.uIdDelete').remove();</script>
				<?php endif;?>
			<?php endif;?>

		<?=$this->nhtml->nFormClose()?>
<?php
	/***************************************************
	 * View Ajax
	 ***************************************************/
?>
		<script type="text/javascript">
			var postValue = {};
			var postValueR01 = {};

			$(function(){


				/***************************************************
	 			* 암호화 키 바인딩
	 			***************************************************/
				postValue['GAesKey'] = GAesKey;


				/***************************************************
	 			* POST 값 바인딩
	 			***************************************************/
				<?=$this->nhtml->getAjaxNameValues('nameValuesPref')?>


				/***************************************************
	 			* 생성,수정
	 			***************************************************/

				<?php

					$sendMode = $mode;
					if($mode == 'retrieve'){
						$sendMode = 'update';
					}
				?>
				$('.uIdBtnConfirm02').click(function(){
					var targetButton = $(this);
					$(targetButton).addClass('disabled');
					var formAjaxSendPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt($sendMode)?>',
						nameValues : nameValuesPref,
						afterCallback:function(data){
							if(data.process == 'validation' && data.resultMessage == 'fail'){
								//console.log('view >> validation, fail');
								$('.nValidation').removeClass('has-error');
								var vStatArray = jQuery.parseJSON(data.vStatArray);
								for(var i = 0; i < vStatArray.length ; i ++){
									var columnName = vStatArray[i]['columnName'];
									$('[name='+columnName+']').closest('.nValidation').addClass('has-error');
								}
								bootbox.alert('유효하지 않은 입력입니다. 입력한 데이터를 확인하세요.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
								return;
							}
							if(data.resultMessage == 'succ' && data.process == 'update'){
								bootbox.alert('데이터가 올바르게 수정 되었습니다.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
							}
							if(data.resultMessage == 'succ' && data.process == 'create'){
								bootbox.alert('데이터가 올바르게 생성 되었습니다.', function(){
									$(targetButton).removeClass('disabled');
									history.go(-1);
									return;
								});
							}
							$('.nValidation').removeClass('has-error');

						}
					};
					//console.log('sendmode >> <?=$sendMode?>');
					Najax.init(formAjaxSendPref);
					Najax.call();
				});


				/***************************************************
	 			* 삭제
	 			***************************************************/
				$('.uIdDelete').click(function(){
					var targetButton = $(this);
					$(targetButton).addClass('disabled');
					bootbox.confirm("정말로 데이터를 삭제 하시겠습니까?", function(result) {
						if(result){
							var formAjaxSendPref = {
								oForm : '#nForm',
								mode : '<?=$this->nurl->encrypt("delete")?>',
								nameValues : nameValuesPref,
								afterCallback:function(data){
									console.log(data);
									$(targetButton).removeClass('disabled');
									if(data.resultMessage == 'succ'){
										//Nlog.put('delete');
										location.href ="/<?=$this->nUri['seg1']?>/<?=$this->nUri['seg2']?>";
									}
								}
							};
							Najax.init(formAjaxSendPref);
							Najax.call();
						}
					});
				});


				/***************************************************
	 			* 목록
	 			***************************************************/
				var listAjaxCallPref = {
					oForm : '#nForm',
					mode : '<?=$this->nurl->encrypt($mode)?>',
					afterCallback:function(data){
						$('.nTable tbody').html(data.listdata);
						$('.nPagination').html(data.pagination);
					}
				};
				Najax.init(listAjaxCallPref);
				Najax.r01('<?=base_url()?><?=$this->nUri["seg1"]?>/<?=$this->nUri["ntxSeg"]?>/?p1=0');

				/* pagination link build */
				var nPaginationP1 = '?p1=0';
				$(document).on('click', '.nPaginationPt', function(event) {
					var dp = $(event.currentTarget).attr('data-nPaginationVal');
					//console.log(dp);
					nPaginationP1 = '?'+dp;

					var nSelectKey = '';
					var nSelectVal = '';
					postValueR01['GAesKey'] = GAesKey;
					nSelectKey = $('.nSelect option:selected').val();
					nSelectVal = $.trim(GAes.enc($('.nSelectVal').val(), GAesKey));
					//console.log('uIdSearchBtn01 clicked');
					//console.log('uIdSearchBtn01 clicked >> key>>' + nSelectKey + '>>val>>' + nSelectVal);
					var listAjaxCallPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt($mode)?>',
						afterCallback:function(data){
							$('.nTable tbody').html(data.listdata);
							$('.nPagination').html(data.pagination);
						}
					};
					Najax.init(listAjaxCallPref);
					Najax.r01('<?=base_url()?><?=$this->nUri["seg1"]?>/<?=$this->nUri["ntxSeg"]?>/'+nPaginationP1+'&<?=trim($this->nurl->encrypt("whereKey"))?>='+nSelectKey+'&<?=trim($this->nurl->encrypt("whereVal"))?>='+nSelectVal);
				});

			});
		</script>